package com.ruoyi.web.controller.market.source;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BaseStatusEnum;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.SourceBaseInfo;
import com.ruoyi.market.enums.InfoTypeEnum;
import com.ruoyi.market.enums.PriceTypeEnum;
import com.ruoyi.source.service.ISourceBaseInfoService;
import com.ruoyi.web.wapper.SourceBaseInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 来源基本Controller
 * 
 * @author ruoyi
 * @date 2024-08-22
 */
@Controller
@RequestMapping("/source/baseInfo")
public class SourceBaseInfoController extends BaseController
{
    private String prefix = "source/baseInfo";

    @Autowired
    private ISourceBaseInfoService sourceBaseInfoService;

    @RequiresPermissions("source:baseInfo:view")
    @GetMapping()
    public String baseInfo()
    {
        return prefix + "/baseInfo";
    }

    /**
     * 查询来源基本列表
     */
    @RequiresPermissions("source:baseInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SourceBaseInfo sourceBaseInfo)
    {
        startPage();
        List<Map<String, Object>> mapList = sourceBaseInfoService.selectSourceBaseInfoListMap(sourceBaseInfo);
        mapList = new SourceBaseInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 导出来源基本列表
     */
    @RequiresPermissions("source:baseInfo:export")
    @Log(title = "来源基本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SourceBaseInfo sourceBaseInfo)
    {
        List<SourceBaseInfo> list = sourceBaseInfoService.selectSourceBaseInfoList(sourceBaseInfo);
        ExcelUtil<SourceBaseInfo> util = new ExcelUtil<SourceBaseInfo>(SourceBaseInfo.class);
        return util.exportExcel(list, "来源基本数据");
    }

    /**
     * 新增来源基本
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("priceTypeList", PriceTypeEnum.getMapList());
        mmap.put("infoTypeList", InfoTypeEnum.getMapList());

        return prefix + "/add";
    }

    /**
     * 新增保存来源基本
     */
    @RequiresPermissions("source:baseInfo:add")
    @Log(title = "来源基本", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SourceBaseInfo sourceBaseInfo)
    {
        return toAjax(sourceBaseInfoService.insertSourceBaseInfo(sourceBaseInfo));
    }

    /**
     * 修改来源基本
     */
    @RequiresPermissions("source:baseInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SourceBaseInfo sourceBaseInfo = sourceBaseInfoService.selectSourceBaseInfoById(id);
        mmap.put("sourceBaseInfo", sourceBaseInfo);

        mmap.put("statusList", BaseStatusEnum.getMapList());
        return prefix + "/edit";
    }

    /**
     * 修改来源基本
     */
    @RequiresPermissions("source:baseInfo:editKey")
    @GetMapping("/editKey/{id}")
    public String editKey(@PathVariable("id") Long id, ModelMap mmap)
    {
        SourceBaseInfo sourceBaseInfo = sourceBaseInfoService.selectSourceBaseInfoById(id);
        mmap.put("sourceBaseInfo", sourceBaseInfo);
        return prefix + "/editKey";
    }

    /**
     * 修改保存来源基本
     */
    @RequiresPermissions("source:baseInfo:edit")
    @Log(title = "来源基本", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SourceBaseInfo sourceBaseInfo)
    {
        return toAjax(sourceBaseInfoService.updateSourceBaseInfo(sourceBaseInfo));
    }

    /**
     * 删除线索
     */
    @RequiresPermissions("source:baseInfo:removeId")
    @Log(title = "删除线索", businessType = BusinessType.UPDATE)
    @PostMapping("/removeId")
    @ResponseBody
    public AjaxResult removeId(@RequestParam("id") Long id) {

        SourceBaseInfo sourceBaseInfo = new SourceBaseInfo();
        sourceBaseInfo.setId(id);
        sourceBaseInfo.setStatus(-1l);

        return toAjax(sourceBaseInfoService.updateSourceBaseInfo(sourceBaseInfo));
    }

    /**
     * 删除来源基本
     */
    @RequiresPermissions("source:baseInfo:remove")
    @Log(title = "来源基本", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sourceBaseInfoService.deleteSourceBaseInfoByIds(ids));
    }
}
